package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/***
 * 交易记录界面**
 *
 * @author llf
 */
public class TradeAct extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private ListView trade;
    //    private TradeAdapter adapter;
//    private Dialog dialog;
//    private RelativeLayout dialog_view;
//    private WheelView year, month;
//    private TextView cancel, sure;
//    private int yearPosition, monthPosition, currentYear, currentMonth;
//    private ArrayList<String> dataList = new ArrayList<String>();
//    private Calendar calendar;
//    private Constant constanse;
    private Context context = TradeAct.this;
    private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private HolderAdapter<HashMap<String, String>> mAdapter;
    private SwipeRefreshLayout mSwipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade);
        initBar();
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("待支付列表");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        tabBack();
        initList();
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.rl_modulename_refresh);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
//		initView();
//		initClick();
    }

    public void initList() {
        loadqueryToPayRecords();
        trade = (ListView) findViewById(R.id.trade_list);
        createAdapter();
    }

    public void createAdapter() {
        HolderAdapter.ViewCreator<HashMap<String, String>> creator = new HolderAdapter.ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {
                return LayoutInflater.from(context).inflate(R.layout.items_listview_trade, null);
            }

            @Override
            public void updateView(View view, int position, final HashMap<String, String> map) {
                Button btn = (Button) view.findViewById(R.id.btn_trade_items_pay);
                TextView tv_trade_items_orderExplain = (TextView) view.findViewById(R.id.tv_trade_items_orderExplain);
                TextView tv_trade_items_orderId = (TextView) view.findViewById(R.id.tv_trade_items_orderId);
                TextView tv_trade_items_orderSum = (TextView) view.findViewById(R.id.tv_trade_items_orderSum);
                tv_trade_items_orderId.setText(map.get("orderId"));
                tv_trade_items_orderExplain.setText(map.get("payContent"));
                tv_trade_items_orderSum.setText(map.get("remain"));
                btn.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, PaymentAct.class);
                        intent.putExtra("toPayId", map.get("toPayId"));
                        context.startActivity(intent);
                    }
                });
            }
        };
        mAdapter = new HolderAdapter<>(this.getLayoutInflater(), creator);
        trade.setAdapter(mAdapter);
        mAdapter.update(data);
    }

    public void loadqueryToPayRecords() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                if (Tool.getString(result, "resultCode").equals("0")) {
                    JSONArray array = Tool.getJsonArray(result, "dataList");
                    data = Tool.getListMapByJsonArray(array);
                    mAdapter.update(data);
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostqueryToPayRecords(context, "", ioAuthCallBack);
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);
                loadqueryToPayRecords();
            }
        }, 2000);
    }

}
